import Cookies from 'js-cookie';
import Config from '@/settings';

const TokenKey = Config.TokenKey;

export function getToken() {
  return Cookies.get(TokenKey);
}

export function setToken(token, rememberMe) {
  if (rememberMe) {
    return Cookies.set(TokenKey, token, { expires: Config.tokenCookieExpires });
  } else return Cookies.set(TokenKey, token);
}

export function customSetToken(key, value) {
  const seconds = 60 * 15;
  const expires = new Date(new Date() * 1 + seconds * 1000);
  return Cookies.set(key, value, { expires: expires });
}

export function getCustomToken(key) {
  return Cookies.get(key);
}

export function removeCustomToken(key) {
  return Cookies.remove(key);
}

export function removeToken() {
  return Cookies.remove(TokenKey);
}

export function setUserId(username) {
  return Cookies.set('iPlanetDirectoryPro', username, { expires: Config.tokenCookieExpires, patch: '/', domain: '.zjiet.edu.cn' });
}

export function clearAllCookie() {
  const keys = document.cookie.match(/[^ =;]+(?=\=)/g);
  if (keys) {
    for (let i = keys.length; i--;) {
      document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString();// 清除当前域名下的,例如：m.kevis.com
      document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString();// 清除当前域名下的，例如 .m.kevis.com
      // document.cookie = keys[i] + '=0;path=/;domain=kevis.com;expires=' + new Date(0).toUTCString()// 清除一级域名下的或指定的，例如 .kevis.com
    }
  }
}

